#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class Solution {
    public:
        int lengthOfLastWord(const char *s) {
            if (!s) return 0;
            int last = -1, cur = 0, len = 0;
            bool wordStart = false;
            while (*s) {
                if (*s == ' ') {
                    if (wordStart) len = cur-last-1;
                    last = cur;    
                    wordStart = false;
                } else {
                    wordStart = true;
                }
                if (*(s+1) == '\0') {
                    if (wordStart) len = cur-last;
                    break;
                }
                cur++;
                s++;
            }

            return len;                        
        }
};

//add better code
    int lengthOfLastWord(const char *s) {
        if (!*s) return 0;
        int len = 0;
        for (int i = 0; *(s + i) != '\0'; ) { 
            while(*(s + i) == ' ' && *(s+i) != '\0') i++;
            if (*(s+i) != '\0')  len = 0;
            while (*(s + i) != ' ' && *(s+i) != '\0') {i++; len++;}
        }   
        return len;
    } 

//add even better code than avove
    int lengthOfLastWord(const char *s) {
        int len = 0;
        while(*s) {
            while(*s == ' ') s++;
            //BUG need to check "b a  "
            if (*s) len = 0;
            while(*s != ' ' && *s != '\0') {s++; len++;}
        }   
        return len;
    } 

//test on 1337 online judge and pass all the test
int main(int argc, char **argv)
{
    Solution mySolution;

    cout << "-----------------Test 1--------------------" << endl;
    cout << mySolution.lengthOfLastWord("HelloWorld") << endl;
    cout << mySolution.lengthOfLastWord(" ") << endl;
    cout << mySolution.lengthOfLastWord("aaa   ") << endl;
    cout << mySolution.lengthOfLastWord(" a") << endl;
    cout << mySolution.lengthOfLastWord("HelloWorld") << endl;


    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
